package com.efast.dmom.equipment.service.repair.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.equipment.bean.repair.EquipPartBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairOrderBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairOrderDetailsBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairOrderRequestBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairOrderReturnBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairerBean;
import com.efast.dmom.equipment.bean.repair.EquipSpareBean;
import com.efast.dmom.equipment.bean.repair.EquipSpareConsumptionBean;
import com.efast.dmom.equipment.dao.repair.EquipRepairInfoDao;
import com.efast.dmom.equipment.dao.repair.EquipRepairOrderDao;
import com.efast.dmom.equipment.dao.repair.EquipRepairSpareConsumptionDao;
import com.efast.dmom.equipment.entity.EquipRepairInfo;
import com.efast.dmom.equipment.entity.EquipSpareConsumption;
import com.efast.dmom.equipment.service.repair.IEquipRepairOrderService;

@Service
public class EquipRepairOrderServiceImpl implements IEquipRepairOrderService {

	@Autowired
	private EquipRepairInfoDao repairInfoDao;

	@Autowired
	private EquipRepairSpareConsumptionDao spareConsumptionDao;

	@Autowired
	private EquipRepairOrderDao repairOrderDao;

	@Override
	public void saveRepairInfo(List<EquipRepairInfo> repairInfo) {

		repairInfoDao.saveRepairInfo(repairInfo);
	}

	@Override
	public void saveRepairSpare(List<EquipSpareConsumption> spareConsumption) {

		spareConsumptionDao.saveRepairSpareConsumption(spareConsumption);
	}

	@Override
	public void deleteRepairOrder(Integer id) {
		if (id > 0) {
			repairOrderDao.deleteRepairOrder(id);
		}
	}

	@Override
	public void editRepairOrderDetail(EquipRepairOrderDetailsBean bean) {
		// 删除维修信息
		repairInfoDao.deleteEquipRepairInfoByOrderId(bean.getOrderId());
		// 删除维修备件信息
		spareConsumptionDao.deleteEquipSpareConsumptionByOrderId(bean.getOrderId());
		List<EquipRepairInfo> repairInfo = bean.getRepairInfoList();
		List<EquipSpareConsumption> consumption = bean.getSpareList();
		// 保存维修信息
		if (repairInfo != null && repairInfo.size() > 0) {
			for (EquipRepairInfo e : repairInfo) {
				e.setOperators(SpringWebUtils.getRequestUser());
				e.setOrderId(bean.getOrderId());
				e.setCompanyCode(SpringWebUtils.getRequestCompany());
				e.setCreateUser(SpringWebUtils.getRequestUser());
			}
			repairInfoDao.saveRepairInfo(repairInfo);
		}
		// 保养维修备件信息
		if (consumption != null && consumption.size() > 0) {
			for (EquipSpareConsumption e : consumption) {
				e.setOrderId(bean.getOrderId());
				e.setCompanyCode(SpringWebUtils.getRequestCompany());
				e.setCreateUser(SpringWebUtils.getRequestUser());
			}
			spareConsumptionDao.saveRepairSpareConsumption(consumption);
		}
	}

	@Override
	public List<EquipRepairerBean> getUserByGroupName(String groupName) {

		return repairOrderDao.getUserByGroupName(groupName);
	}

	@Override
	public void updateRepairOrderStatus(Integer orderId, String status) {

		repairOrderDao.updateRepairOrderStatus(orderId, status);
	}

	@Override
	public EquipRepairOrderReturnBean getRepairOrderByStatus(EquipRepairOrderRequestBean requestBean) {

		List<EquipRepairOrderBean> orderList = repairOrderDao.getRepairOrderByStatus(requestBean);

		EquipRepairOrderReturnBean returnBean = new EquipRepairOrderReturnBean();
		returnBean.setResultList(orderList);
		returnBean.setTotalRows(orderList.size());

		return returnBean;
	}

	@Override
	public EquipRepairOrderBean getRepairOrderDetails(Integer orderId) {

		EquipRepairOrderBean orderBean = repairOrderDao.getRepairOrderDetails(orderId);
		
		List<EquipSpareConsumptionBean> consumptionList = repairOrderDao.getConsumptionByOrderId(orderId);
		List<EquipRepairerBean>  repairerList = repairOrderDao.getRepairerByOrderId(orderId);
		
		orderBean.setComsuptionList(consumptionList);
		orderBean.setRepairerList(repairerList);
		
		return orderBean;
	}

	@Override
	public List<EquipSpareBean> getEquipSpare(String partName) {

		return repairOrderDao.getEquipSpare(partName);
	}
	
	@Override
	public List<EquipPartBean> getEquipPart(String category) {
		
		return repairOrderDao.getEquipPart(category);
	}
	
	@Override
	public void updateSpareInventory(String spareName, Integer count) {
		
		repairOrderDao.updateSpareInventory(spareName, count);
	}

	@Override
	public void saveRepairOrder(EquipRepairOrderBean orderBean) {

		Integer orderId = orderBean.getOrderId();

		repairOrderDao.deleteConsumptionByOrderId(orderId);
		repairOrderDao.deleteRepairerInfoByOrderId(orderId);

		orderBean.setLastUpdateTime(new Date());
		orderBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
		repairOrderDao.updateRepairOrder(orderBean);
		
		List<EquipSpareConsumptionBean> consumptionList = orderBean.getComsuptionList();
		
		if(consumptionList != null && consumptionList.size() > 0){
			
			for(EquipSpareConsumptionBean bean : consumptionList){
				
				bean.setOrderId(orderId);
				bean.setLastUpdateTime(new Date());
				bean.setLastUpdateUser(SpringWebUtils.getRequestUser());
				bean.setCompanyCode(SpringWebUtils.getRequestCompany());
				bean.setCreateUser(SpringWebUtils.getRequestUser());
			}
			repairOrderDao.addConsumptionList(consumptionList);
		}
		
		List<EquipRepairerBean> repairerList = orderBean.getRepairerList();
		
		if(repairerList != null && repairerList.size() > 0){
			
			for(EquipRepairerBean bean : repairerList ){
				
				bean.setOrderId(orderId);
				bean.setLastUpdateTime(new Date());
				bean.setLastUpdateUser(SpringWebUtils.getRequestUser());
				bean.setCompanyCode(SpringWebUtils.getRequestCompany());
				bean.setCreateUser(SpringWebUtils.getRequestUser());
			}
			
			repairOrderDao.addRepairerList(repairerList);
		}
		
	}

}
